package com.yami.shop.api.controller.tudu;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.model.tudu.Combination;
import com.yami.shop.bean.model.tudu.Commodity;
import com.yami.shop.bean.model.tudu.SmallBasket;
import com.yami.shop.dao.tudu.CombinationMapper;
import com.yami.shop.dao.tudu.CommodityMapper;
import com.yami.shop.dao.tudu.SmallBasketMapper;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.tudu.CombinationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/p/combination")
@Api(tags = "组合接口")
public class CombinationController {

    @Autowired
    private CombinationService combinationService;
    @Autowired
    private CombinationMapper combinationMapper;
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private SmallBasketMapper smallBasketMapper;

    @PostMapping("/zuhe")
    @ApiOperation(value = "小购物车组合商品", notes = "小购物车组合商品")
    public String zuhe(Long[] smallids){
        List<Long> ids = Arrays.asList(smallids);//数组转list
        String userId = SecurityUtils.getUser().getUserId();
        SmallBasket small = smallBasketMapper.selectById(ids.get(0));
        int cont;
        String res;//组合码
        do{
            res = combinationService.createShareCode();
            cont = combinationMapper.selectCount(new LambdaQueryWrapper<Combination>()
                    .eq(Combination::getCombinationId,res));
        }while (cont > 0);
        Combination combination = new Combination();
        combination.setCombinationId(res);
        combination.setUserId(userId);
        combination.setMode(small.getMode());
        combination.setPic("组合图片");
        int res1 = combinationMapper.insert(combination);//创建组合包
        if (res1 == 0){
            return "创建组合包失败";
        }
        for (Long id : ids) {
            SmallBasket sma = smallBasketMapper.selectById(id);
            Commodity commodity = new Commodity();
            commodity.setCommodityId(sma.getCommodityId());
            commodity.setCommodityCount(sma.getBasketCount());
            commodity.setCombinationId(res);
            int res2 = commodityMapper.insert(commodity);//将小购物商品插入被组合商品表
            int res3 = smallBasketMapper.deleteById(sma.getId());//删除小购物车商品
            if (res2 == 0){
                return "将小购物商品插入被组合商品表失败";
            }
            if (res3 == 0){
                return "删除小购物车商品失败";
            }
        }
        if(combination.getMode() == 3){
            SmallBasket smallBasket = new SmallBasket();
            smallBasket.setCommodityId(-1);
            smallBasket.setUserId(userId);
            smallBasket.setCommodityType(2);
            smallBasket.setMode(combination.getMode());//购买方式
            smallBasket.setCombinationId(res);//组合码
            smallBasket.setBasketCount(1);
            int res4 = smallBasketMapper.insert(smallBasket);//将组合插入到购物车内
            if (res4 == 0){
                return "将组合插入到购物车内失败";
            }
        }

        return res;
    }

    @PostMapping("/shanchuzuhe")
    @ApiOperation(value = "小购物车删除组合商品", notes = "小购物车删除组合商品，传入购物车id和组合包id")
    public ResponseEntity<Boolean> shanchuzuhe(SmallBasket small){
        smallBasketMapper.deleteById(small.getId());//删除小购物车组合
        combinationMapper.delete(new LambdaQueryWrapper<Combination>()
        .eq(Combination::getCombinationId,small.getCombinationId()));//删除组合包
        commodityMapper.delete(new LambdaQueryWrapper<Commodity>()
        .eq(Commodity::getCombinationId,small.getCombinationId()));//删除被组合商品表的商品
        return ResponseEntity.ok(true);
    }
}
